package com.rich.web.servlet;

import java.util.List;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.rich.dao.CategoryDao;
import com.rich.domain.Category;
import com.rich.domain.Product;
import com.rich.service.CategoryService;
import com.rich.service.ProductServer;
import com.rich.utils.BeanFactory;

/**
 * Servlet implementation class AdminProduct
 */
@WebServlet("/adminProduct")
public class AdminProduct extends BaseServlet {
	private static final long serialVersionUID = 1L;
	
	ProductServer ps = (ProductServer) BeanFactory.getBean("ProductService");
	
	/**
	 * 获取所有上架的商品
	 * @param request
	 * @param response
	 * @return
	 */
	public String findAll(HttpServletRequest request, HttpServletResponse response) {
		try {
			//获取所有商品的信息
			List<Product> products = ps.findAll();
			//把所有商品放到域对象中
			request.setAttribute("products", products);
			//跳转页面
			return "/admin/product/list.jsp";
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException();
		}
	}
	
	/**
	 * 转发到添加商品的页面中
	 */
	public String addUI(HttpServletRequest request, HttpServletResponse response) {
		try {
			//查询所有分类
			CategoryDao cd = (CategoryDao) BeanFactory.getBean("CategoryDao");
			List<Category> cList = cd.findAll();
			request.setAttribute("cList", cList);
			return "/admin/product/add.jsp";
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException();
		}
	}
	
}
